package nsq_setting

import (
	"GraduationProject/public/db"
	"GraduationProject/public/models"
	"GraduationProject/webserver/controllers/util"
	"github.com/gin-gonic/gin"
)

// todo: 暂停 Nsq
func Pause(c *gin.Context) {
	_, err := util.AdminFilter(c)
	if err != nil {
		return
	}
	nsqSettingID, err := util.PostFormInt(c, "nsq_setting_id")
	if err != nil {
		return
	}

	if db.DB.Model(&models.NsqSetting{}).Where("id = ?", nsqSettingID).
		Update("status", models.NsqSettingStatusPause).RowsAffected == 0 {
		util.FailedResponse(c, 10001, "暂停失败")
		return
	}

	// todo: 设置redis
	//db.Redis.HSet(models.WorkerStatusKey, strconv.Itoa(workerID), models.WorkerPause)

	util.SuccessResponse(c)
}

func Begin(c *gin.Context) {
	_, err := util.AdminFilter(c)
	if err != nil {
		return
	}
	nsqSettingID, err := util.PostFormInt(c, "nsq_setting_id")
	if err != nil {
		return
	}

	if db.DB.Model(&models.NsqSetting{}).Where("id = ?", nsqSettingID).
		Update("status", models.NsqSettingStatusRunning).RowsAffected == 0 {
		util.FailedResponse(c, 10001, "启动失败")
		return
	}

	// todo: 设置redis
	//db.Redis.HSet(models.WorkerStatusKey, strconv.Itoa(workerID), models.WorkerRunning)

	util.SuccessResponse(c)
}
